<template>
  <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="'配置主要信息'"
             append-to-body width="1120px">
    <el-row :gutter="15">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-col :span="24">
          <el-form-item label="模板内容" prop="templateBody">
            <el-input v-model="form.templateBody" rows="10" type="textarea" :autosize="{minRows: 10, maxRows: 20}"
                      :style="{width: '100%'}"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类别" prop="modelType">
            <el-select v-model="form.modelType" placeholder="类别" class="filter-item" style="width: 245px;">
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="模板文件名" prop="velocityFile">
            <el-input v-model="form.velocityFile" style="width: 245px;"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="输出主目录" prop="mainPath">
            <el-input v-model="form.mainPath" style="width: 245px;"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="输出包名" prop="packagePath">
            <el-input v-model="form.packagePath" style="width: 245px;"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="输出文件名" prop="saveFileName">
            <el-input v-model="form.saveFileName" style="width: 245px;"/>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
    import {
        queryGeneratorCodeConfigByCondition,
        updateGeneratorCodeConfig,
        saveColumnMysqlJavaConfigSingleLineData,
        addNewTemplateFileAndConfig
    } from '@/api/code-tools'
    import {queryGeneratorCodeFileRuleByCondition} from '@/api/GeneratorCodeFileRule'

    export default {
        components: {},
        data() {
            return {
                loading: false, dialog: false,
                form: {
                    id: '',
                    templateBody: '',
                    modelType: '',
                    velocityFile: '',
                    mainPath: '',
                    packagePath: '',
                    saveFileName: ''
                },
                rules: {
                    modelType: [
                        {required: true, message: '请输入模块类别', trigger: 'blur'}
                    ],
                    velocityFile: [
                        {required: true, message: '请输入模板文件名', trigger: 'blur'}
                    ],
                    saveFileName: [
                        {required: true, message: '请输入文件导出名', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            cancel() {
                this.resetForm();
            },
            doSubmit() {
                let data = this.form;
                let id = data.id;
                console.log(id);
                if (id) {
                    console.log(this.form.modelType)
                    //this.doUpdate(data);
                } else {
                    console.log(this.form.modelType);
                    console.log(data);
                    this.doSave(data);
                }
            },
            doUpdate(data) {
                updateGeneratorCodeConfig(data).then(res => {
                    this.doQueryGeneratorCodeConfig(data);
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false;
                    console.log(err.response.data.message);
                })
            },
            doSave(data) {
                addNewTemplateFileAndConfig(data).then(res => {
                    this.doQueryGeneratorCodeFileRule(data);
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        this.$notify({
                            title: res.data,
                            type: 'success',
                            duration: 2500
                        })
                    }
                }).catch(err => {
                    this.loading = false;
                    console.log(err.response.data.message);
                })
            },
            resetForm() {
                this.dialog = false;
                this.$refs['form'].resetFields();
            },
            doQueryGeneratorCodeFileRule(data) {
                queryGeneratorCodeFileRuleByCondition(data).then(res => {
                    console.log(this.form);
                    console.log(res.data);
                    if (res.hasError) {
                        this.$notify({
                            title: res.error.message,
                            type: 'fail',
                            duration: 2500
                        })
                    } else {
                        if (res.data === '') {
                            res.data = {}
                        }
                        this.form = res.data;
                        console.log(this.form)
                    }
                    this.form.modelType = data.modelType;
                    console.log(this.form.modelType)

                }).catch(err => {
                    this.loading = false;
                    console.log(err.response.data.message)
                })
            },
            doTransmitVariable(data) {
                this.form.modelType = data.modelType;
            }
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
